package com.ruoyi.points.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.points.domain.PointsLotteryPrize;
import com.ruoyi.points.mapper.PointsLotteryActivityMapper;
import com.ruoyi.points.domain.PointsLotteryActivity;
import com.ruoyi.points.service.IPointsLotteryActivityService;

/**
 * 积分抽奖活动Service业务层处理
 * 
 * @author nz
 * @date 2025-08-01
 */
@Service
public class PointsLotteryActivityServiceImpl implements IPointsLotteryActivityService 
{
    @Autowired
    private PointsLotteryActivityMapper pointsLotteryActivityMapper;

    /**
     * 查询积分抽奖活动
     * 
     * @param id 积分抽奖活动主键
     * @return 积分抽奖活动
     */
    @Override
    public PointsLotteryActivity selectPointsLotteryActivityById(Long id)
    {
        return pointsLotteryActivityMapper.selectPointsLotteryActivityById(id);
    }

    /**
     * 查询积分抽奖活动列表
     * 
     * @param pointsLotteryActivity 积分抽奖活动
     * @return 积分抽奖活动
     */
    @Override
    public List<PointsLotteryActivity> selectPointsLotteryActivityList(PointsLotteryActivity pointsLotteryActivity)
    {
        return pointsLotteryActivityMapper.selectPointsLotteryActivityList(pointsLotteryActivity);
    }

    /**
     * 新增积分抽奖活动
     * 
     * @param pointsLotteryActivity 积分抽奖活动
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPointsLotteryActivity(PointsLotteryActivity pointsLotteryActivity)
    {
        pointsLotteryActivity.setCreateTime(DateUtils.getNowDate());
        int rows = pointsLotteryActivityMapper.insertPointsLotteryActivity(pointsLotteryActivity);
        insertPointsLotteryPrize(pointsLotteryActivity);
        return rows;
    }

    /**
     * 修改积分抽奖活动
     * 
     * @param pointsLotteryActivity 积分抽奖活动
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePointsLotteryActivity(PointsLotteryActivity pointsLotteryActivity)
    {
        pointsLotteryActivity.setUpdateTime(DateUtils.getNowDate());
        pointsLotteryActivityMapper.deletePointsLotteryPrizeByActivityId(pointsLotteryActivity.getId());
        insertPointsLotteryPrize(pointsLotteryActivity);
        return pointsLotteryActivityMapper.updatePointsLotteryActivity(pointsLotteryActivity);
    }

    /**
     * 批量删除积分抽奖活动
     * 
     * @param ids 需要删除的积分抽奖活动主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePointsLotteryActivityByIds(Long[] ids)
    {
        pointsLotteryActivityMapper.deletePointsLotteryPrizeByActivityIds(ids);
        return pointsLotteryActivityMapper.deletePointsLotteryActivityByIds(ids);
    }

    /**
     * 删除积分抽奖活动信息
     * 
     * @param id 积分抽奖活动主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePointsLotteryActivityById(Long id)
    {
        pointsLotteryActivityMapper.deletePointsLotteryPrizeByActivityId(id);
        return pointsLotteryActivityMapper.deletePointsLotteryActivityById(id);
    }

    /**
     * 新增积分抽奖奖品信息
     * 
     * @param pointsLotteryActivity 积分抽奖活动对象
     */
    public void insertPointsLotteryPrize(PointsLotteryActivity pointsLotteryActivity)
    {
        List<PointsLotteryPrize> pointsLotteryPrizeList = pointsLotteryActivity.getPointsLotteryPrizeList();
        Long id = pointsLotteryActivity.getId();
        if (StringUtils.isNotNull(pointsLotteryPrizeList))
        {
            List<PointsLotteryPrize> list = new ArrayList<PointsLotteryPrize>();
            for (PointsLotteryPrize pointsLotteryPrize : pointsLotteryPrizeList)
            {
                pointsLotteryPrize.setActivityId(id);
                list.add(pointsLotteryPrize);
            }
            if (list.size() > 0)
            {
                pointsLotteryActivityMapper.batchPointsLotteryPrize(list);
            }
        }
    }
}
